<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button
            type="primary"
            size="small"
            @click="addPermission(1, '0')"
          >添加1级权限</el-button>
        </div>

        <el-table border :data="permissionList" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="scoped">
              <el-button
                v-if="scoped.row.type === 1"
                type="text"
                @click="addPermission(2, scoped.row.id)"
              >添加2权限</el-button>
              <el-button
                type="text"
                @click="editFn(scoped.row.id)"
              >编辑</el-button>
              <el-button
                type="text"
                @click="delFn(scoped.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 弹窗 -->
      <per-dialog
        ref="perDialog"
        :type="type"
        :pid="pid"
        :show-dialog="showDialog"
        :form-data-obj="formData"
        :is-edit="isEdit"
        @close="dialogCloseFn"
        @confirmEv="confirmFn"
      />
    </div>
  </div>
</template>

<script>
import { getPermissionListAPI, addPermissionAPI, getPermissionDetailAPI, updatePermissionAPI } from '@/api'
import { delPermissionAPI } from '@/api'
import { transTree } from '@/utils'
import perDialog from './components/perDialog.vue'
export default {
  components: {
    perDialog
  },
  data() {
    return {
      permissionList: [], // 权限管理列表数据
      showDialog: false, // 控制弹出框是否显示
      type: 0, // 添加权限的类型 1一级 2二级
      pid: 0, // 要添加权限点这个对象要用pid值(上级的id值)
      isEdit: false, // 编辑状态
      formData: {} // 权限点的信息 传子组件回显 (编辑时用)
    }
  },
  created() {
    // 获取权限点
    this.getPermissionListFn()
  },
  methods: {
    // 获取权限点 -> 铺设页面
    async getPermissionListFn() {
      const res = await getPermissionListAPI().catch((err) => err)
      if (!res.success) return this.$message.error(res.message)
      this.$refs.perDialog.permissionList = res.data
      this.permissionList = transTree(res.data, '0')
      console.log(this.permissionList)
    },

    // 子组件传出来的关闭弹窗的事件
    dialogCloseFn() {
      this.showDialog = false
      this.isEdit = false
    },

    // 点击添加的事件
    addPermission(type, id) {
      this.showDialog = true
      // 记录当前添加的关键信息
      this.type = type
      this.pid = id
    },

    // 子组件点击确认的事件
    async confirmFn(data) {
      if (this.isEdit) {
        const res = await updatePermissionAPI(data).catch(err => err)
        if (!res.success) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.showDialog = false
        this.isEdit = false
      } else {
        const res = await addPermissionAPI(data).catch(err => err)
        if (!res.success) return this.$message.error(res.message)
        this.getPermissionListFn()
        this.$message.success(res.message)
        this.showDialog = false
      }
      this.getPermissionListFn()
    },

    // 点击编辑的事件
    async editFn(id) {
      this.isEdit = true
      this.showDialog = true
      const res = await getPermissionDetailAPI(id).catch(err => err)
      if (!res.success) return this.$message.error(res.message)
      this.formData = res.data
    },
    // 删除权限
    async delFn(id) {
      // 确认消息
      const confirmRes = await this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      if (confirmRes === 'cancel') return this.$message.info('您取消了删除')

      // 调用接口，并传入 ID
      const res = await delPermissionAPI(id)
      if (!res.success) return this.$message.error(res.message)
      this.$message.success(res.message)
      this.getPermissionListFn()
    }
  }
}
</script>

<style lang="scss" scoped></style>
